package arithmetic.sort;

/**
 * 希尔排序
 */
class ShellSort {

    /**
     * 希尔排序,对插入排序的优化 10000个数 5ms
     */
    public static void shellSort(Integer[] array) {
        int gap, i, j, n = array.length;
        for (gap = n / 2; gap > 0; gap /= 2) {
            // 直接插入排序
            for (i = 0; i < gap; i++) {
                // 对每个分组排序，每次从1A到1E，从2A到2E...
                for (j = i + gap; j < n; j = j + gap) {
                    if (array[j] < array[j - gap]) {
                        int currentValue = array[j];
                        int position = j - gap;
                        while (position >= 0 && array[position] > currentValue) {
                            array[position + gap] = array[position];
                            position -= gap;
                        }
                        array[position + gap] = currentValue;
                    }
                }
            }
        }
    }

    /**
     * 改进 10000个数 6ms
     */
    public static void shellSort2(Integer[] array) {
        int gap, i, j, n = array.length;
        for (gap = n / 2; gap > 0; gap /= 2) {
            // 改成从1B开始，先和1A比较，然后取2B与2A比较，再取1C与前面自己组内的数据比较…….
            for (j = gap; j < n; j++) {
                if (array[j] < array[j - gap]) {
                    int currentValue = array[j];
                    int position = j - gap;
                    while (position >= 0 && array[position] > currentValue) {
                        array[position + gap] = array[position];
                        position -= gap;
                    }
                    array[position + gap] = currentValue;
                }
            }
        }
    }

    public static void shellSort3(Integer[] array) {
        int gap, i, j, n = array.length;
        for (gap = n / 2; gap > 0; gap /= 2) {
            // 改成从1B开始，先和1A比较，然后取2B与2A比较，再取1C与前面自己组内的数据比较…….
            for (j = gap; j < n; j++) {
                if (array[j] < array[j - gap]) {
                    int currentValue = array[j];
                    int position = j - gap;
                    while (position >= 0 && array[position] > currentValue) {
                        array[position + gap] = array[position];
                        position -= gap;
                    }
                    array[position + gap] = currentValue;
                }
            }
        }
    }

    /**
     * 改进2 10000个数 6ms
     */
    public static void shellSort4(Integer[] array) {
        int gap, i, j, n = array.length;
        for (gap = n / 2; gap > 0; gap /= 2) {
            for (i = gap; i < n; i++) {
                for (j = i - gap; j >= 0 && array[j] > array[j + gap]; j -= gap) //每个组内的比较
                {
                    swap(j, j + gap, array);
                }
            }
        }
    }

    /**
     * 交换数组内ab的位置
     */
    public static void swap(int a, int b, Integer[] array) {
//		System.out.println(a+":"+array[a]+" "+b+":"+array[b]);
        array[a] = array[a] + array[b];
        array[b] = array[a] - array[b];
        array[a] = array[a] - array[b];
//		printlnArr(array);
    }

    public static void printlnArr(Integer[] a) {
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i]);
            System.out.print(", ");
        }
        System.out.println();
    }
}
